package com.gla.market.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gla.market.R;
import com.gla.market.constant.Constants;
import com.gla.market.entity.User;
import com.gla.market.fragments.AboutMe;
import com.gla.market.utils.NetUtil;
import com.gla.market.utils.SharedPreferencesUtil;
import com.lidroid.xutils.util.LogUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class UptoVipActivity extends AppCompatActivity {

    private Button upToVip_shengji,upToVip_back;
    private Context mContext;
    private Integer user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upto_vip);
        mContext = this;
        user_id = (Integer) SharedPreferencesUtil.get(mContext, "userInfo", "user_id", -1);
        int is_vip = (int) SharedPreferencesUtil.get(mContext, "userInfo", "is_vip", -1);
        initView();
        //返回按钮
        upToVip_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if(is_vip==1){
            upToVip_shengji.setClickable(false);
            upToVip_shengji.setText("已开通会员");
        } else {
            upToVip_shengji.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tobe_vip();
                }
            });
        }
    }
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case -2:
                    //未登录，需要登录！
                    Toast.makeText(mContext, "您还未登录！请先登录！", Toast.LENGTH_SHORT).show();
                    break;
                case -1:
                    //网络请求失败
                    Toast.makeText(mContext, "网络请求失败！"
                            , Toast.LENGTH_SHORT).show();
                    break;
                case 0:
                    //网络请求成功，但是返回状态为失败
                    Toast.makeText(mContext, msg.obj == null ? "请求数据失败！"
                            : msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    SharedPreferencesUtil.put(mContext,"userInfo","is_vip",1);
                    Toast.makeText(mContext, "恭喜你成为尊贵的超级萌宠会员！\n\t去购买商品吧！", Toast.LENGTH_SHORT).show();
                    //发送修改个人页信息广播
                    Intent intent = new Intent(AboutMe.UPDATE);
                    mContext.sendBroadcast(intent);
                    finish();
                    break;
                default:
                    break;
            }

        }
    };
    private void tobe_vip() {
        String url = Constants.BASE_URL + Constants.UPGRADE_VIP + "?user_id="+user_id;
        NetUtil.doGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = Message.obtain();
                message.what = -1;
                uiHandler.sendMessage(message);
                Log.e("失败", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //将服务器响应的参数response.body().string())发送到hanlder中，并更新ui
                Message message = Message.obtain();
                String jsonStr = response.body().string().trim();
                JSONObject object = JSON.parseObject(jsonStr);
                Log.i("调试", ""+jsonStr);
                if (!object.getString("message").contains("JWT") && object.getInteger("code")==100) {
                    message.what = 1;
                    message.obj = object.getString("message");
                } else {
                    message.what = -2;
                }
                uiHandler.sendMessage(message);
            }
        });
    }

    private void initView() {
        upToVip_back = findViewById(R.id.upToVip_back);
        upToVip_shengji = findViewById(R.id.upToVip_shengji);
    }

}